package com.medical.Service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.medical.DTO.AppointmentsDTO;
import com.medical.DTO.AppointmentsPageDTO;
import com.medical.Mapper.AppointmentsMapper;
import com.medical.Service.AppointmentsService;
import com.medical.VO.AppointmentsVO;
import com.medical.result.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class AppointmentsServiceImpl implements AppointmentsService {
    @Autowired
    private AppointmentsMapper appointmentsMapper;

    /*
    * 添加预约
    * @param appointments
     */
    @Override
    @Transactional
    public void addAppointments(AppointmentsDTO appointments) {
        log.info("添加预约：{}", appointments);
        appointments.setCreatedDate(LocalDateTime.now());
        if (appointments.getAppointmentDate() == null) {
            appointments.setAppointmentDate(LocalDateTime.now());
        }
        appointmentsMapper.insert(appointments);
    }
    
    /*

    * 更新预约
    * @param appointments
     */
    @Override
    @Transactional
    public void updateAppointments(AppointmentsDTO appointments) {
        log.info("更新预约：{}", appointments);
        appointmentsMapper.update(appointments);
    }
    
    /*
    * 删除预约
    * @param id
     */
    @Override
    @Transactional
    public void deleteAppointments(Integer id) {
        log.info("删除预约，id：{}", id);
        appointmentsMapper.delete(id);
    }
    
    /*
    * 通过id查询预约
    * @param id
    * @return
     */
    @Override
    public AppointmentsDTO getById(Integer id) {
        log.info("根据id查询预约，id：{}", id);
        return appointmentsMapper.getById(id);
    }
    
    /*
    * 分页查询预约列表
    * @AppointmentsDTO
    * @return
     */

    @Override
    public PageResult pageQuery(AppointmentsPageDTO appointments) {
        PageHelper.startPage(appointments.getPage(),appointments.getPageSize());
        List<AppointmentsVO> appointmentsList=appointmentsMapper.list(appointments);
        Page<AppointmentsVO> page= (Page<AppointmentsVO>) appointmentsList;
        return new PageResult(page.getTotal(),page.getResult());
    }

    /*
    * 更新状态
    * @param id
    * @param status
     */
    @Override
    @Transactional
    public void updateStatus(Integer id, String status) {
        log.info("更新预约状态，id：{}，status：{}", id, status);
        appointmentsMapper.updateStatus(id, status);
    }
}
